import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/Register/RegisterIndex.vue'),
      meta: {
        title: '注册',
        show: false
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login/LoginIndex.vue'),
      meta: {
        title: '登录',
        show: false
      }
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/Home/HomeIndex.vue'),
      meta: {
        title: '首页',
        show: true
      }
    },
    {
      path: '/search/:keyword?',
      name: 'search',
      component: () => import('@/views/search/SearchIndex.vue'),
      meta: {
        title: '搜索',
        show: true
      }
    },
    {
      path: '/detail/:id',
      name: 'detail',
      component: () => import('@/views/Detail/DetailIndex.vue'),
      meta: {
        title: '详情',
        show: true
      }
    },
    {
      path: '/addcartsuccess',
      name: 'addcartsuccess',
      component: () => import('@/views/AddCartSuccess/AddCartSuccess.vue'),
      meta: {
        title: '添加购物车成功',
        show: true
      }
    },
    {
      path: '/shopcart',
      name:'shopcart',
      component: () => import('@/views/ShopCart/ShopCart.vue'),
    },
    {
      path: '/trade',
      name:'trade',
      component: () => import('@/views/Trade/TradeIndex.vue')
    },
    {
      path: '/pay',
      name:'pay',
      component: () => import('@/views/Pay/PayIndex.vue'),
      beforeEnter: (to, from, next) => {
        if (from.path === '/trade') {
          next()
        } else {
          next('/home')
        }
      }
    },
    {
      path: '/paysuccess',
      name:'paysuccess',
      component: () => import('@/views/PaySuccess/PaySuccess.vue'),
    },
    {
      path: '/myorder',
      name:'myorder',
      component: () => import('@/views/MyOrder/MyOrder.vue')
    },
  
  ],

  // 路由切换时滚动条位置
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0, left: 0 }
    }
  }

})

export default router;